package com.qf.common.persistence;


import lombok.Data;

import java.util.List;

/**
 * 分页类
 */
@Data
public class Page<T> {
    private int pageNo;
    private int pageSize = 20; // 默认每页显示数量
    private int count; // 总数量
    private int first = 1;
    private int last;
    private int prev;
    private int next;
    private boolean firstPage;
    private boolean lastPage;
    private List<T> list;

    public void initialize() {
        if (this.pageSize < 1) {
            this.pageSize = 20; // 确保pageSize不小于1
        }

        this.last = (int) Math.ceil((double) this.count / this.pageSize);

        if (this.pageNo < this.first) { //如果当前页码小于首页页码，那么将当前页码设置为首页页码，并标记为第一页
            this.pageNo = this.first;
            this.firstPage = true;
        } else if (this.pageNo > this.last) { //如果当前页码大于尾页页码，那么将当前页码设置为尾页页码，并标记为最后一页
            this.pageNo = this.last;
            this.lastPage = true;
        } else { //如果当前页码在首页和尾页之间，那么它既不是第一页也不是最后一页
            this.firstPage = false;
            this.lastPage = false;
        }

        // 设置上一页和下一页
        this.prev = (this.pageNo > this.first) ? this.pageNo - 1 : this.first;
        this.next = (this.pageNo < this.last) ? this.pageNo + 1 : this.last;
    }

    /**
     * 设置本页数据对象列表
     *
     * @param list
     */
    public void setList(List<T> list) {
        this.list = list;
        this.count = list.size();
        initialize();
    }
}
